#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char* reserve_words(char* words)
{
    int len = strlen(words);
    printf("%s, len = %d\n", words, len);

    int wb,we,wl;
    wb=we=0;
    
    char *res_words = (char*)malloc(len+1);
    res_words += len;
    *res_words = '\0';
    
    char *p = words;
    for(; *p!='\0'; ++p, ++we) {
        --res_words;
        if(*p == ' ') {
            *res_words = ' ';
            int wl = we-wb;
            strncpy(res_words+1, p-wl, wl);
            wb = we+1;
        }
    }
    wl = we-wb;
    strncpy(res_words, p-wl, wl);

    return res_words;        
}

int main()
{
    char *words = "This is a test";
    char *res = reserve_words(words);
    printf("after reserve: %s\n", res);
    return 0;
}
